package cn.wolfcode.shop.mq;

import cn.wolfcode.shop.consts.MQConstents;
import cn.wolfcode.shop.web.controller.OrderInfoController;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 消除售完标记，要将分布式集群中的每一个阶段都消除
 */
@Component
@Slf4j
@RocketMQMessageListener(
        topic = MQConstents.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstents.CLEAN_STOCKCOUNT_FLAG_TAG,
        consumerGroup = MQConstents.CLEAN_STOCKCOUNT_FLAG_CONSUMER_GROUP,
        messageModel = MessageModel.BROADCASTING    // 广播模式
)
public class CleanStockCountFlagListener implements RocketMQListener<Long> {

    @Override
    public void onMessage(Long id) {
        log.info("收到清除本地售完标记的消息：{}", id);
        // 修改存储标记的常量 ConcurrentHashMap 中的数据，true表示有库存
        OrderInfoController.STOCK_COUNT_STATE_MAP.put(id, true);
    }
}
